﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMutex>
#include <QMessageBox>
#include <QString>
#include <QTimer>
#include <QListView>
#include <QFileDialog>
#include <QQueue>
#include <QVector>

#include <opencv2/opencv.hpp>

#pragma execution_character_set("utf-8")

#include "fsConfig.h"
#include "savethread.h"
#include "uim.h"
#include "mvs.h"
#include "tool.h"
#include "edityt88.h"
#include "uimsetup.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

// using namespace cv;
using namespace std;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void mvsFrameCallback(unsigned char *pImageData, MVS_FRAME_OUT *pFrameOut);
    void mvsEventCallback(MVS_EVENT_OUT *pEventOut);

protected:
    void hideEvent(QHideEvent *ev) override;
    void showEvent(QShowEvent *ev) override;
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;
    void closeEvent(QCloseEvent *event) override;
    void moveEvent(QMoveEvent *event) override;
    void mouseMoveEvent(QMouseEvent *ev) override;
    void resizeEvent(QResizeEvent *event) override;
    bool eventFilter(QObject *watched, QEvent *event)  override;

private:
    Ui::MainWindow *ui;

    QMutex *saveImageMutex;
    //窗口
    HWND    winFrameWnd;
    int     winFrameWidth;
    int     winFrameHeight;
    HWND    winResultWnd;
    int     winResultWidth;
    int     winResultHeight;
    //采集
    SaveThread *saveImageThread;
    bool    isInitPreview;
    int     curPreviewPos;
    int     curResultFileNum;
    cv::Mat curPreviewImage;
    //采集帧图像
    int curFrameImageZoom;
    int curFrameImagePosX,curFrameImagePosY;
    cv::Mat curFrameImage;
    QMutex *frameImageZoomMutex;
    //结果图像
    int curResultImageZoom;
    int curResultImagePosX,curResultImagePosY;
    bool isResultImage;
    cv::Mat curResultImage;
    QMutex *resultImageZoomMutex;
    //轮廓处理
    bool bAutoProc;
    bool isProc;
    QTimer *procTimer;
    cv::Mat curProcSrcImage;
    cv::Mat curProcShowImage;
    float curProcScale;
    QVector<cv::Rect>curProcBox;

    //结果文件
    QStringList curResultDirList;
    int curResultDirIndex;
    QStringList curResultFileList;
    int curResultFileNo[2];
    int curResultFileIndex;

    void init(void);
    void exit(void);
    void closeAll(void);
    void initVar(void);
    void saveVar(void);
    void initConfVar(void);
    void initUimVar(void);
    void initMvsVar(bool isShow=true);
    void initScreen(void);
    void initUimScreen(void);
    void initMvsScreen(void);

    void initTime(void);
    void initEncodeTimer(void);
    void initCanMoveTimer(void);
    void initProcTime(void);

    bool isMvs(void);
    void imageResize(void);
    void initPreview(void);
    void relePreview(void);
    void clearPreview(void);
    void addPreview(const cv::Mat src);
    void savePreview(void);
    void flushFrame(void);

    void flushLogo(void);
    void flushSplit(void);
    void flushPhotoButton(void);
    void flushOther(void);
    void flushProc(void);

    void flushMvsState(void);
    void flushCanState(void);
    void flushAutoPhotoInfo(void);
    void flushPushButton(QPushButton *button, bool checked);
    bool getCanFocusLimitPos(const int direction=0);

    void showTopLineRate(void);
    void showTopFrameGrabing(void);
    void showTopImageNums(void);
    void showTopErrorNums(void);
    void showTopLostNums(void);
    void showTopLinkRate(void);
    void showTopFrameScreen(void);
    void showTopEncodeCounter(void);

    void showBottomInfo(const QString msg);
    void showBottomAlarm(const QString msg);
    void showBottomMvsStatus(const QString msg);
    void showBottomCanStatus(const QString msg);
    void showBottomResult(const QString msg);

    bool setCanFar(int node,int rate,int step);
    void canMove(const QString msg,const bool mode,const int direction);
    void canMovePos(const QString msg,const int locate,int rate=0);
    void canMoveStep(const QString msg,const int step,int rate=0);
    void canStopMove(void);

    void flushResultFileList(const QString curFile);
    void addResultFileList(QFileInfo info);
    void delResultFileListItem(int index);
    void clearResultFile(void);
    void showResult(void);
    void flushResult(void);
    void flushResultInfo(void);

    QString getCurProcDir(void);
    void clearProcFile(void);
    void flushProcInfo(void);
    void showContour(const cv::Mat src,const cv::Mat image,const float scale);
    int procContour(const cv::Mat image,const float scale,QVector<cv::Rect> &boxs);

private:
    QTimer *bottomTimer;
    QTimer  *encodeTimer;
    QTimer  *canMoveTimer;
    qint64  curCanMoveTimer;
    bool isKeyCanMove;
    QString labelTopInfo[8];

    fsConfig *fsConf;
    UIM *fsUim;
    MVS *fsMvs;

    int     winWidth;
    int     winHeight;
    QList<int> splitterPhotoSizes;
    QList<int> splitterMainSizes;
    QList<int> splitterWindowSizes;

    bool    bAutoPhoto;
    bool    bAutoPhotoFirst;
    qint64  autoPhotoMoveTimer;

    //回调函数
    void canCallback(void);
    void RcvRtcnNotify(unsigned int dwDevIndex, P_CAN_MSG_OBJ pRtcnMsgObj, unsigned int dwMsgLen);
    static void StaticRcvRtcnNotify(unsigned int dwDevIndex, P_CAN_MSG_OBJ pRtcnMsgObj, unsigned int dwMsgLen);

signals:
    void SendRTCNMsg(QString,int,int);

private slots:
    //电机设置处理
    void recvUimFlush(void);
    void recvUimSave(void);
    void retCanMsg(QString,int,int);
    //存储线程
    void recvSaveThreadFile(QString);
    //鼠标
    void recvFrameImageMove(int,int);
    void recvResultImageMove(int,int);
    void recvFrameImageZoom(int,int,int);
    void recvResultImageZoom(int,int,int);
    //定时器
    void showBottomTimer(void);
    void updateCanEncodeTimer(void);
    void updateCanMoveTimer(void);
    void updateProcTimer(void);
    //窗口
    void on_splitterPhoto_splitterMoved(int pos, int index);
    void on_splitterWindow_splitterMoved(int pos, int index);
    void on_splitterMain_splitterMoved(int pos, int index);
    //设备启动及设置
    void on_pushButtonDeviceDriven_clicked();
    void on_pushButtonElectricDriven_clicked();
    void on_pushButtonSetup_clicked();
    void on_comboBoxDevice_currentIndexChanged(int index);
    //电机操作
    void on_pushButtonFocusUp_clicked();
    void on_pushButtonFocusUp_pressed();
    void on_pushButtonFocusUp_released();
    void on_pushButtonFocusDown_clicked();
    void on_pushButtonFocusDown_pressed();
    void on_pushButtonFocusDown_released();
    void on_pushButtonFocusSet_clicked();
    void on_pushButtonFocusReset_clicked();
    //自拍
    void on_pushButtonRightMove_clicked();
    void on_spinBoxRightRate_valueChanged(int arg1);
    void on_spinBoxAutoPhotoCanRate_valueChanged(int arg1);
    void on_checkBoxAutoPhoto_toggled(bool checked);
    void on_checkBoxEncodeStop_toggled(bool checked);
    //图像存储
    void on_spinBoxSavePara_valueChanged(int arg1);
    void on_comboBoxSaveFormat_currentIndexChanged(int index);
    void on_checkBoxSaveHeight_toggled(bool checked);
    void on_comboBoxSaveDpi_currentTextChanged(const QString &arg1);
    //相机
    void on_pushButtonStartAcquisition_clicked();
    void on_comboBoxImageFormat_currentIndexChanged(int index);
    void on_spinBoxMvsWidth_valueChanged(int arg1);
    void on_spinBoxMvsHeight_valueChanged(int arg1);
    void on_spinBoxMvsLineRate_valueChanged(int arg1);
    void on_checkBoxMvsLineRate_toggled(bool checked);
    void on_doubleSpinBoxMvsExposure_valueChanged(double arg1);
    void on_checkBoxTriggerMode_toggled(bool checked);
    void on_spinBoxPreDivider_valueChanged(int arg1);
    void on_spinBoxMultiplier_valueChanged(int arg1);
    void on_spinBoxPostDivider_valueChanged(int arg1);
    void on_checkBoxPRNUCEnable_toggled(bool checked);
    //目录
    void on_pushButtonSelectResultPath_clicked();
    void on_pushButtonSelectImagePath_clicked();
    void on_comboBoxResultDir_currentIndexChanged(int index);
    void on_pushButtonDirFlush_clicked();
    void on_pushButtonDirNew_clicked();
    void on_pushButtonDirRen_clicked();
    void on_pushButtonDirDel_clicked();
    void on_pushButtonDirClear_clicked();
    void on_listWidgetResultFile_currentRowChanged(int currentRow);
    //拍照
    void on_pushButtonPhotograph_clicked();
    //采集处理
    void on_checkBoxNonContourTwoBorder_toggled(bool checked);
    void on_spinBoxContourThreshold_valueChanged(int arg1);
    void on_spinBoxContourGaussian_valueChanged(int arg1);
    void on_spinBoxContourMinWidth_valueChanged(int arg1);
    void on_spinBoxContourMinHeight_valueChanged(int arg1);
    void on_spinBoxUpDownBorder_valueChanged(int arg1);
    void on_spinBoxLeftRightBorder_valueChanged(int arg1);
    void on_spinBoxImageMaxWidth_valueChanged(int arg1);
    void on_pushButtonAutoContour_clicked();
    //开启识别轮廓
    void on_checkBoxContourRecognition_toggled(bool checked);

    void on_pushButtonContourAdd_clicked();

    void on_pushButtonContourCut_clicked();

private:
    /*SoftYT88 加密狗机密策略
     * 软件公钥KEY-，芯片ID-32HEX，锁ID-16HEX，存储字符串-64HEX
     * 读取chipId,读取lockId
     * 生成加密串-64HEX
     *
     --*/
    const int softKeyPublic=20240229;
    SoftkeyYT88 *softKey;
    editYt88 *keyEdit;

    int getSoftKey(void);
    int getSoftKeyVer(void);
    QString getSoftKeyChipId(void);
    QString getSoftKeyId(void);
    QString getSoftKeyPass(void);


};
#endif // MAINWINDOW_H
